#TODO: namespace same as server side code
# client/server_proxy/impl/jsonrpclib

from typing import Any, Callable
from bcat.util.str import Str
from bcat.rpc.client.server_proxy_impl import IServerProxyImpl

import jsonrpclib

class JsonRpcLibServerProxyImpl(IServerProxyImpl):
    def __init__(self, url: str):
        self.__url = url
        self.__server = jsonrpclib.Server(url)
        self.__functions = {}

    def stub(self)-> Any:
        return self.__server

    def register(self, name: str, func: Callable) -> bool:
        if Str(name).is_not_empty() and name not in self.__functions:
            self.__functions[name] = func
            return True
        return False
    def unregister(self, name: str) -> bool:
        if name in self.__functions:
            del self.__functions[name]
            return True
        return False

import unittest 
class TestJsonRpcLibImpl(unittest.TestCase):
    def test_jsonrpclib(self):
        url = "http://127.0.0.1:80"
        jspi = JsonRpcLibServerProxyImpl(url)
        jspi.stub().tick()
        print(jsonrpclib.history.request)
